package com.iweb.tmall.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iweb.tmall.entity.Brand;
import com.iweb.tmall.entity.CategoryBrandRelation;
import com.iweb.tmall.global.ErrorEnum;
import com.iweb.tmall.global.ResponseData;
import com.iweb.tmall.mapper.BrandMapper;
import com.iweb.tmall.param.BrandParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;
    @Override
    public ResponseData getList(BrandParam param) {
        ResponseData responseData = null;
        Page<Brand> page = new Page<>(param.getCurrent(), param.getSize());
        LambdaQueryWrapper<Brand> qw = getLambdaQueryWrapper();
        qw.like(param.getName()!= null,Brand::getName,param.getName());
        qw.eq(param.getIsDelete()!= null ,Brand::getIsDelete,param.getIsDelete());
         page = brandMapper.selectPage(page, qw);
         responseData = new ResponseData(page);
        return responseData;
    }

    @Override
    public ResponseData deleteById(Integer id) {
        ResponseData responseData = null;
        int row = brandMapper.logicDeleteById(id);
        if (row > 0){
            responseData = new ResponseData("删除成功");
        }else {
            responseData = new ResponseData(ErrorEnum.DATABASE_SERVICE_ERROR);
        }
        return responseData;
    }

    @Override
    public ResponseData updateById(Brand entity) {
        ResponseData responseData = null;
        int row = brandMapper.updateById(entity);
        if (row > 0){
            responseData = new ResponseData(entity);
        }else {
            responseData = new ResponseData(ErrorEnum.DATABASE_SERVICE_ERROR);
        }

        return responseData;
    }

    @Override
    public ResponseData save(Brand entity) {
        ResponseData responseData = null;
        int row = brandMapper.insert(entity);
        if (row > 0){
            responseData = new ResponseData(entity);
        }else {
            responseData = new ResponseData(ErrorEnum.DATABASE_SERVICE_ERROR);
        }
        return responseData;
    }

    @Override
    public ResponseData getById(Integer id) {
       return null;
    }

    @Override
    public ResponseData check(String name) {
        ResponseData responseData = null;
        LambdaQueryWrapper<Brand> qw = getLambdaQueryWrapper();
        qw.eq(Brand::getName,name);
        Brand one = brandMapper.selectOne(qw);
        responseData = new ResponseData(one);
        return responseData;
    }

    @Override
    public ResponseData addRelation(Integer brandId, Integer categoryId) {
        ResponseData responseData = null;
        Integer row = brandMapper.addRelation(brandId, categoryId);
        if (row>0){
            responseData = new ResponseData("绑定成功");
        }else {
            responseData = new ResponseData(ErrorEnum.DATABASE_ERROR);
        }
        return responseData;
    }

    @Override
    public ResponseData loadBrandList() {
        ResponseData responseData = null;
        Brand brand = new Brand();
        List<Brand> list = brandMapper.selectList(getLambdaQueryWrapper(brand));
        responseData = new ResponseData(list);
        return responseData;
    }

    @Override
    public ResponseData validRelation(Integer brandId,Integer categoryId) {
        ResponseData responseData = null;
        Integer row = brandMapper.validRelation(brandId, categoryId);
        if (row>0){
            responseData = new ResponseData("1");
        }else {
            responseData = new ResponseData("0");
        }
        return responseData;
    }
}
